Food Delivery Time Prediction¶

Week Two Project Two¶

In [1]:
import pandas as pd
import numpy as np
import plotly.express as px

Loading the Dataset¶

In [2]:
dataset = pd.read_csv("deliverytime.txt")
In [3]:
dataset.head()
Out[3]:
ID Delivery_person_ID Delivery_person_Age Delivery_person_Ratings Restaurant_latitude Restaurant_longitude Delivery_location_latitude Delivery_location_longitude Type_of_order Type_of_vehicle Time_taken(min)
0 4607 INDORES13DEL02 37 4.9 22.745049 75.892471 22.765049 75.912471 Snack motorcycle 24
1 B379 BANGRES18DEL02 34 4.5 12.913041 77.683237 13.043041 77.813237 Snack scooter 33
2 5D6D BANGRES19DEL01 23 4.4 12.914264 77.678400 12.924264 77.688400 Drinks motorcycle 26
3 7A6A COIMBRES13DEL02 38 4.7 11.003669 76.976494 11.053669 77.026494 Buffet motorcycle 21
4 70A2 CHENRES12DEL01 32 4.6 12.972793 80.249982 13.012793 80.289982 Snack scooter 30
In [4]:
dataset.shape
Out[4]:
(45593, 11)
In [5]:
dataset.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 45593 entries, 0 to 45592
Data columns (total 11 columns):
 #   Column                       Non-Null Count  Dtype  
---  ------                       --------------  -----  
 0   ID                           45593 non-null  object 
 1   Delivery_person_ID           45593 non-null  object 
 2   Delivery_person_Age          45593 non-null  int64  
 3   Delivery_person_Ratings      45593 non-null  float64
 4   Restaurant_latitude          45593 non-null  float64
 5   Restaurant_longitude         45593 non-null  float64
 6   Delivery_location_latitude   45593 non-null  float64
 7   Delivery_location_longitude  45593 non-null  float64
 8   Type_of_order                45593 non-null  object 
 9   Type_of_vehicle              45593 non-null  object 
 10  Time_taken(min)              45593 non-null  int64  
dtypes: float64(5), int64(2), object(4)
memory usage: 3.8+ MB
In [6]:
dataset.isnull().sum()
Out[6]:
ID                             0
Delivery_person_ID             0
Delivery_person_Age            0
Delivery_person_Ratings        0
Restaurant_latitude            0
Restaurant_longitude           0
Delivery_location_latitude     0
Delivery_location_longitude    0
Type_of_order                  0
Type_of_vehicle                0
Time_taken(min)                0
dtype: int64

Calculating the Distance betweeen Two Latitudes and Longitudes Using Haversine formula¶

In [7]:
from math import radians, cos, sin, asin, sqrt
In [8]:
def dist_cal(lat1,lon1,lat2,lon2):
    
    lat1=radians(lat1)
    lon1=radians(lon1) 
    lat2=radians(lat2)
    lon2=radians(lon2)
    
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
    c = 2 * asin(sqrt(a))
    r=6371
    return(c * r)


for i in range(len(dataset)):
    dataset.loc[i, 'Distance'] = dist_cal(dataset.loc[i, 'Restaurant_latitude'], 
                                        dataset.loc[i, 'Restaurant_longitude'], 
                                        dataset.loc[i, 'Delivery_location_latitude'], 
                                        dataset.loc[i, 'Delivery_location_longitude']) 
In [9]:
dataset.head()
Out[9]:
ID Delivery_person_ID Delivery_person_Age Delivery_person_Ratings Restaurant_latitude Restaurant_longitude Delivery_location_latitude Delivery_location_longitude Type_of_order Type_of_vehicle Time_taken(min) Distance
0 4607 INDORES13DEL02 37 4.9 22.745049 75.892471 22.765049 75.912471 Snack motorcycle 24 3.025149
1 B379 BANGRES18DEL02 34 4.5 12.913041 77.683237 13.043041 77.813237 Snack scooter 33 20.183530
2 5D6D BANGRES19DEL01 23 4.4 12.914264 77.678400 12.924264 77.688400 Drinks motorcycle 26 1.552758
3 7A6A COIMBRES13DEL02 38 4.7 11.003669 76.976494 11.053669 77.026494 Buffet motorcycle 21 7.790401
4 70A2 CHENRES12DEL01 32 4.6 12.972793 80.249982 13.012793 80.289982 Snack scooter 30 6.210138

Exploring the Data Using Plotly¶

In [10]:
figure = px.scatter(data_frame = dataset, 
                    x="Delivery_person_Age",
                    y="Time_taken(min)", 
                    size="Time_taken(min)", 
                    color = "Distance",
                    trendline="ols", 
                    title = "Relationship Between Time Taken and Age")
figure.show()
In [11]:
figure = px.scatter(data_frame=dataset,
                    x="Delivery_person_Ratings",
                    y="Time_taken(min)",
                    size="Time_taken(min)",
                    color = "Distance",
                    trendline="ols",
                    title = "Relationship Between Time Taken and Delivery_person_Ratings")
figure.show()
In [12]:
figure = px.scatter(data_frame=dataset,
                    x="Delivery_person_Age",
                    y="Delivery_person_Ratings",
                    size="Delivery_person_Age",
                    color = "Delivery_person_Ratings",
                    trendline="ols",
                    title = "Relationship Between Delivery_person_Age and Delivery_person_Ratings")
figure.show()
In [13]:
fig = px.box(dataset, 
             x="Type_of_vehicle",
             y="Time_taken(min)",
             color="Type_of_order")
fig.show()

Using the feautures¶

1-"age of the delivery partner"

2-"ratings of the delivery partner"

3-"distance between the restaurant and the delivery location"

That contribute more to the Delivery time, Let's predict Delivery Time for our input

In [14]:
pip install keras
Requirement already satisfied: keras in c:\users\user\anaconda3\lib\site-packages (2.11.0)
Note: you may need to restart the kernel to use updated packages.
In [15]:
pip install tensorflow
Requirement already satisfied: tensorflow in c:\users\user\anaconda3\lib\site-packages (2.11.0)
Requirement already satisfied: tensorflow-intel==2.11.0 in c:\users\user\anaconda3\lib\site-packages (from tensorflow) (2.11.0)
Requirement already satisfied: opt-einsum>=2.3.2 in c:\users\user\anaconda3\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (3.3.0)
Requirement already satisfied: typing-extensions>=3.6.6 in c:\users\user\anaconda3\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (4.3.0)
Requirement already satisfied: setuptools in c:\users\user\anaconda3\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (63.4.1)
Requirement already satisfied: astunparse>=1.6.0 in c:\users\user\anaconda3\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (1.6.3)
Requirement already satisfied: packaging in c:\users\user\anaconda3\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (21.3)
Requirement already satisfied: google-pasta>=0.1.1 in c:\users\user\anaconda3\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (0.2.0)
Requirement already satisfied: tensorboard<2.12,>=2.11 in c:\users\user\anaconda3\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (2.11.2)
Requirement already satisfied: h5py>=2.9.0 in c:\users\user\anaconda3\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (3.7.0)
Requirement already satisfied: libclang>=13.0.0 in c:\users\user\anaconda3\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (15.0.6.1)
Requirement already satisfied: numpy>=1.20 in c:\users\user\anaconda3\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (1.21.5)
Requirement already satisfied: tensorflow-estimator<2.12,>=2.11.0 in c:\users\user\anaconda3\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (2.11.0)
Requirement already satisfied: grpcio<2.0,>=1.24.3 in c:\users\user\anaconda3\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (1.51.1)
Requirement already satisfied: six>=1.12.0 in c:\users\user\anaconda3\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (1.16.0)
Requirement already satisfied: gast<=0.4.0,>=0.2.1 in c:\users\user\anaconda3\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (0.4.0)
Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in c:\users\user\anaconda3\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (0.29.0)
Requirement already satisfied: termcolor>=1.1.0 in c:\users\user\anaconda3\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (2.2.0)
Requirement already satisfied: absl-py>=1.0.0 in c:\users\user\anaconda3\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (1.4.0)
Requirement already satisfied: keras<2.12,>=2.11.0 in c:\users\user\anaconda3\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (2.11.0)
Requirement already satisfied: protobuf<3.20,>=3.9.2 in c:\users\user\anaconda3\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (3.19.6)
Requirement already satisfied: flatbuffers>=2.0 in c:\users\user\anaconda3\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (23.1.4)
Requirement already satisfied: wrapt>=1.11.0 in c:\users\user\anaconda3\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (1.14.1)
Requirement already satisfied: wheel<1.0,>=0.23.0 in c:\users\user\anaconda3\lib\site-packages (from astunparse>=1.6.0->tensorflow-intel==2.11.0->tensorflow) (0.37.1)
Requirement already satisfied: markdown>=2.6.8 in c:\users\user\anaconda3\lib\site-packages (from tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (3.3.4)
Requirement already satisfied: requests<3,>=2.21.0 in c:\users\user\anaconda3\lib\site-packages (from tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (2.28.1)
Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in c:\users\user\anaconda3\lib\site-packages (from tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (1.8.1)
Requirement already satisfied: google-auth<3,>=1.6.3 in c:\users\user\anaconda3\lib\site-packages (from tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (2.16.0)
Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in c:\users\user\anaconda3\lib\site-packages (from tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (0.4.6)
Requirement already satisfied: tensorboard-data-server<0.7.0,>=0.6.0 in c:\users\user\anaconda3\lib\site-packages (from tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (0.6.1)
Requirement already satisfied: werkzeug>=1.0.1 in c:\users\user\anaconda3\lib\site-packages (from tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (2.0.3)
Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in c:\users\user\anaconda3\lib\site-packages (from packaging->tensorflow-intel==2.11.0->tensorflow) (3.0.9)
Requirement already satisfied: rsa<5,>=3.1.4 in c:\users\user\anaconda3\lib\site-packages (from google-auth<3,>=1.6.3->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (4.9)
Requirement already satisfied: pyasn1-modules>=0.2.1 in c:\users\user\anaconda3\lib\site-packages (from google-auth<3,>=1.6.3->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (0.2.8)
Requirement already satisfied: cachetools<6.0,>=2.0.0 in c:\users\user\anaconda3\lib\site-packages (from google-auth<3,>=1.6.3->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (5.2.1)
Requirement already satisfied: requests-oauthlib>=0.7.0 in c:\users\user\anaconda3\lib\site-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (1.3.1)
Requirement already satisfied: idna<4,>=2.5 in c:\users\user\anaconda3\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (3.3)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\user\anaconda3\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (2022.9.14)
Requirement already satisfied: charset-normalizer<3,>=2 in c:\users\user\anaconda3\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (2.0.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\users\user\anaconda3\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (1.26.11)
Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in c:\users\user\anaconda3\lib\site-packages (from pyasn1-modules>=0.2.1->google-auth<3,>=1.6.3->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (0.4.8)
Requirement already satisfied: oauthlib>=3.0.0 in c:\users\user\anaconda3\lib\site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (3.2.2)
Note: you may need to restart the kernel to use updated packages.

Food Delivery Time Prediction Model Using Sklearn,Keras,Tensorflow¶

In [14]:
from keras.models import Sequential
from keras.layers import Dense, LSTM
In [15]:
from sklearn.model_selection import train_test_split
x = np.array(dataset[["Delivery_person_Age", 
                   "Delivery_person_Ratings", 
                   "Distance"]])
y = np.array(dataset[["Time_taken(min)"]])
xtrain, xtest, ytrain, ytest = train_test_split(x, y,test_size=0.15, random_state=50)

model = Sequential()
model.add(LSTM(128, return_sequences=True, input_shape= (xtrain.shape[1], 1)))
model.add(LSTM(64, return_sequences=False))
model.add(Dense(1))
model.summary()
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 lstm (LSTM)                 (None, 3, 128)            66560     
                                                                 
 lstm_1 (LSTM)               (None, 64)                49408     
                                                                 
 dense (Dense)               (None, 1)                 65        
                                                                 
=================================================================
Total params: 116,033
Trainable params: 116,033
Non-trainable params: 0
_________________________________________________________________
In [16]:
model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(xtrain, ytrain, batch_size=1, epochs=9)
Epoch 1/9
38754/38754 [==============================] - 199s 5ms/step - loss: 70.3584
Epoch 2/9
38754/38754 [==============================] - 196s 5ms/step - loss: 63.5416
Epoch 3/9
38754/38754 [==============================] - 211s 5ms/step - loss: 61.2916
Epoch 4/9
38754/38754 [==============================] - 197s 5ms/step - loss: 60.3270
Epoch 5/9
38754/38754 [==============================] - 202s 5ms/step - loss: 60.0479
Epoch 6/9
38754/38754 [==============================] - 188s 5ms/step - loss: 59.8902
Epoch 7/9
38754/38754 [==============================] - 191s 5ms/step - loss: 59.5017
Epoch 8/9
38754/38754 [==============================] - 210s 5ms/step - loss: 59.0108
Epoch 9/9
38754/38754 [==============================] - 201s 5ms/step - loss: 59.1098
Out[16]:
<keras.callbacks.History at 0x21d134f2b50>
In [18]:
print("Food Delivery Time Prediction")
a = int(input("Age of Delivery Partner: "))
b = float(input("Ratings of Previous Deliveries(1-5): "))
c = int(input("Total Distance in (KM): "))

features = np.array([[a, b, c]])
print("Predicted Delivery time in Minutes= ",model.predict(features))
Food Delivery Time Prediction
Age of Delivery Partner: 25
Ratings of Previous Deliveries(1-5): 4
Total Distance in (KM): 25
1/1 [==============================] - 0s 24ms/step
Predicted Delivery time in Minutes=  [[37.430412]]
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]: